home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6186 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Easiest way to center a string?
  5. Date: Fri, 23 Feb 96 00:02:17 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <825033737snz@genesis.demon.co.uk>
  9. References: <4fobka$1st@parlor.hiwaay.net> <DMqEG5.8z4@eskimo.com> <Pine.A32.3.91.960221162940.161086A-100000@black.weeg.uiowa.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <Pine.A32.3.91.960221162940.161086A-100000@black.weeg.uiowa.edu>
  16.            robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
  17.  
  18. >On 21 Feb 1996, Tanmoy Bhattacharya wrote:
  19. >
  20. >> The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes: 
  21. >> <snip>
  22. >>    > In article Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes: 
  23. >>    > <snip>
  24. >>    >      That's equivalent to strlen(String + -LineLen), which is in turn 
  25. >>    >    equivalent to strlen(String[-LineLen]), and the expression
  26. > String[-80] is 
  27. >> 
  28. >>                              ^ this is now a char, not a char*.
  29. >
  30. >  Certainly is.  My apologies for slipping up here.  For some 
  31. >unaccountable reason I was thinking a[i] was equivalent to (a + i) and not 
  32. >*(a + i).
  33. >
  34. >>    >      If you meant (strlen(String) - LineLen) / 2 then you will get a 
  35. >>    >    negative length specifier for any string with fewer than LineLen 
  36. >>    >    characters.  If you meant (LineLen - strlen(String) / 2, then your 
  37. >>    > strlen returns size_t which is guaranteed unsigned. (I am assuming
  38. >>    > LineLen is not long.)
  39. >
  40. >>      Right, but strlen(String) would be 12, minus LineLen which is 80, which 
  41. >>    is -68, divided by two is -34, which is the number passed as a width 
  42. >
  43. >> but if size_t is unsigned int or unsigned long, and LineLen is int,
  44. >> the result is not -68 because an unsigned quantity cannot be -68. (My
  45. >> parenthetical remark was wrong: I had assumed that size_t was at least
  46. >> as long as unsigned int and LineLen is not long).
  47. >
  48. >  I guess I still need to have this clarified (my knowledge of type 
  49. >promotion et al is admittedly shaky).  If you subtract an unsigned int 
  50. >from an int, is the result necessarily unsigned int?
  51.  
  52. Yes, this follows from the rules for the 'usual arithmetic conversions'.
  53. These ensure 2 operands are promoted to a common type. I won't list them
  54. out in full right now but if one operand is a signed long and the other
  55. an unsigned long the result is unsigned long. Similarly for signed and
  56. unsigned int. In that respect I believe the ANSI rules match the old
  57. K&R rules.
  58.  
  59. >Is that true 
  60. >generally for integral types?
  61.  
  62. All chars and shorts promote to int except where an int can't represent all
  63. of the values of an unsigned char or unsigned short. in that case it
  64. promotes to unsigned int. The other 'usual arithmetic conversion' rules are
  65. then applied to these promoted types.
  66.  
  67. > IOW, are you saying that strlen("hello, 
  68. >world") - 80 will evaluate to UINT_MAX - 68 (or something like that)?
  69.  
  70. I made a mistake. -68U evaluates to (UINT_MAX+1)-68, not UINT_MAX-68.
  71.  
  72. If size_t corresponds to unsigned int or unsigned long that is correct.
  73. If it corresponds to unsigned short or unsigned char and int is wider
  74. then the result will have type int and value -68. If you want to ensure that
  75. value you should write (int)sizeof("hello, world") - 80
  76.  
  77. -- 
  78. -----------------------------------------
  79. Lawrence Kirby | fred@genesis.demon.co.uk
  80. Wilts, England | 70734.126@compuserve.com
  81. -----------------------------------------
  82.